#include "../include/weeding_robot_gui/settings.h"
#include "ui_settings.h"

settings::settings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::settings)
{
    ui->setupUi(this);
    /**
      * Setting Frame Default Settings
      *
    */
    QSettings video_topic_settings("video topic settings", "weeding_robot_gui");
    QStringList names = video_topic_settings.value("names").toStringList();
    QStringList topics = video_topic_settings.value("topics").toStringList();

    QSettings settings("Qt-Ros Package", "weeding_robot_gui");
    QString master_url = settings.value("master_url",QString("http://192.168.1.2:11311/")).toString();
    QString host_url = settings.value("host_url", QString("192.168.1.3")).toString();
    bool use_enviroment_variables = settings.value("use_environment_variables", false).toBool();
    bool remember_settings = settings.value("remember_settings", bool(true)).toBool();

    QSettings topics_setting("topics Settings", "weeding_robot_gui");
    QString zed_camera_topic = topics_setting.value("zed camera topic", QString("/zed_2/zed_left/image_raw")).toString();
    QString depth_image_topic = topics_setting.value("depth image topic", QString("/zed_2/zed_node/depth")).toString();
    QString bbox_message_topic = topics_setting.value("bbox message topic", QString("/tensorrt_yolov4/bbox")).toString();
    QString detection_result_topic = topics_setting.value("detection result topic", QString("/weeding_robot/detection_result")).toString();
    QString actuator_topic = topics_setting.value("actuator topic", QString("/actuator/geometry")).toString();
    QString velocity_control_topic = topics_setting.value("velocity control topic", QString("/cmd_vel")).toString();

    if(names.size()==4)
    {
        ui->lineEdit_image_1_name->setText(names[0]);
        ui->lineEdit_image_2_name->setText(names[1]);
        ui->lineEdit_image_3_name->setText(names[2]);
        ui->lineEdit_image_4_name->setText(names[3]);
    }
    if(topics.size()==4)
    {
        ui->lineEdit_image_1_topic_name->setText(topics[0]);
        ui->lineEdit_image_2_topic_name->setText(topics[1]);
        ui->lineEdit_image_3_topic_name->setText(topics[2]);
        ui->lineEdit_image_4_topic_name->setText(topics[3]);
    }

    ui->lineEdit_actuator_topic->setText(actuator_topic);
    ui->lineEdit_zed_camera_topic->setText(zed_camera_topic);
    ui->lineEdit_detection_result_topic->setText(detection_result_topic);
    ui->lineEdit_depth_image_topic->setText(depth_image_topic);
    ui->lineEdit_bbox_message_topic->setText(bbox_message_topic);
    ui->lineEdit_velocity_control_topic->setText(velocity_control_topic);

    ui->lineEdit_Host_IP->setText(host_url);
    ui->lineEdit_ROS_Master_URL->setText(master_url);
    ui->checkBox_Use_enviroment_variables->setChecked(use_enviroment_variables);
    ui->checkBox_Remember_settings_on_startup->setChecked(remember_settings);

    connections();
}

void settings::connections()
{
    connect(ui->pushButton_Cancel, SIGNAL(clicked(bool)), this, SLOT(close()));
    connect(ui->pushButton_OK, SIGNAL(clicked(bool)), this, SLOT(slot_writtingSettings()));
    connect(ui->pushButton_Apply, SIGNAL(clicked(bool)), this, SLOT(slot_writtingSettings()));

    //按钮设置
    connect(ui->pushButton_Settings_ROS_Master,SIGNAL(clicked(bool)),this,SLOT(slot_Settings_pushButton_ROS_Master()));
    connect(ui->pushButton_Terminal, SIGNAL(clicked(bool)),this,SLOT(slot_Settings_pushButton_terminal()));
    connect(ui->pushButton_Topic, SIGNAL(clicked(bool)),this,SLOT(slot_Settings_pushButton_topic()));
    connect(ui->pushButton_Video_Settings,SIGNAL(clicked(bool)),this,SLOT(slot_Settings_pushButton_video_topic()));
}

void settings::slot_Settings_pushButton_ROS_Master()
{
    ui->tabWidget->setCurrentWidget(ui->tab_1_ROS_Master_Settings);
}

void settings::slot_Settings_pushButton_terminal()
{
    ui->tabWidget->setCurrentWidget(ui->tab_4_Terminal_Settings);
}

void settings::slot_Settings_pushButton_topic()
{
    ui->tabWidget->setCurrentWidget(ui->tab_2_Topic_Settings);
}

void settings::slot_Settings_pushButton_video_topic()
{
    ui->tabWidget->setCurrentWidget(ui->tab_3_Display_Settings);
}

void settings::slot_writtingSettings()
{
    QSettings topics_setting("topics Settings", "weeding_robot_gui");
    topics_setting.setValue("zed camera topic", ui->lineEdit_zed_camera_topic->text());
    topics_setting.setValue("depth image topic", ui->lineEdit_depth_image_topic->text());
    topics_setting.setValue("bbox message topic", ui->lineEdit_bbox_message_topic->text());
    topics_setting.setValue("detection result topic", ui->lineEdit_detection_result_topic->text());
    topics_setting.setValue("actuator topic", ui->lineEdit_actuator_topic->text());
    topics_setting.setValue("velocity control topic", ui->lineEdit_velocity_control_topic->text());

    QSettings video_topic_settings("video topic settings", "weeding_robot_gui");
    QStringList names,topics;
    names.append(ui->lineEdit_image_1_name->text());
    names.append(ui->lineEdit_image_2_name->text());
    names.append(ui->lineEdit_image_3_name->text());
    names.append(ui->lineEdit_image_4_name->text());
    topics.append(ui->lineEdit_image_1_topic_name->text());
    topics.append(ui->lineEdit_image_2_topic_name->text());
    topics.append(ui->lineEdit_image_3_topic_name->text());
    topics.append(ui->lineEdit_image_4_topic_name->text());
    video_topic_settings.setValue("names", names);
    video_topic_settings.setValue("topics", topics);


    QSettings settings("Qt-Ros Package", "weeding_robot_gui");
    settings.setValue("master_url",ui->lineEdit_ROS_Master_URL->text());
    settings.setValue("host_url", ui->lineEdit_Host_IP->text());
    settings.setValue("use_environment_variables", ui->checkBox_Use_enviroment_variables->isChecked());
    settings.setValue("remember_settings", ui->checkBox_Remember_settings_on_startup->isChecked());

    QMessageBox::critical(NULL, "保存成功！", "保存成功，需重启后生效！", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    this->close();
}

settings::~settings()
{
    delete ui;
}
